local Badge = require "widgets/badge"
local UIAnim = require "widgets/uianim"

local obrsword_powerBadge = Class(Badge, function(self, owner, art)
    Badge._ctor(self, "obrsword_power", owner)
    self:SetHAnchor(0) -- 设置原点x坐标位置，0、1、2分别对应屏幕中、左、右
    self:SetVAnchor(1)
   -- self:SetSize(60, 100)
    self:SetPosition(950, -200, 0)
    local oldOnControl = self.OnControl
    self.OnControl = function(self, control, down)
        if self.focus and control == CONTROL_SECONDARY then
            if down then
                self:StartDrag()
            else
                self:EndDrag()
            end
        end
        if oldOnControl then
            return oldOnControl(self, control, down)
        end
    end



    self.SetDragPosition = function(self, pos)
        --[[local pos
        if type(x) == "number" then
            pos = Vector3(x, y, z)
        else
            pos = x
        end]]

        local self_scale = self:GetScale()

        local newpos = self.p_startpos + (pos - self.m_startpos) / (self_scale.x)
        self:SetPosition(newpos)
    end

    self.StartDrag = function(self)
        if not self.followhandler then
            local mousepos = TheInput:GetScreenPosition()
            self.m_startpos = mousepos           --鼠标初始坐标
            self.p_startpos = self:GetPosition() --面板初始坐标
            self.followhandler = TheInput:AddMoveHandler(function(x, y)
                self:SetDragPosition(Vector3(x, y, 0))
                if not Input:IsMouseDown(MOUSEBUTTON_RIGHT) then
                    self:EndDrag()
                end
            end)
            self:SetDragPosition(mousepos)
        end
    end

    self.EndDrag = function(self)
        if self.followhandler then
            self.followhandler:Remove()
        end
        self.followhandler = nil
        self.m_startpos = nil
        self.p_startpos = nil
    end
end)

function obrsword_powerBadge:SetPercent(val, max)
    Badge.SetPercent(self, val, max)
end

return obrsword_powerBadge
